//
//  ClubEditSquareNoticeViewController.swift
//  77Club
//
//  Created by Apple on 2024/12/27.
//

import UIKit

class ClubEditSquareNoticeViewController: ClubBaseViewController {

    private let backButton = c_make_ex(UIButton.self)
        .image("Common_blackBack").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .text("论坛公告").color(.black).font(16.font_semiBold)
        .alignment(.center).end()
    private let saveButton = c_make_ex(UIButton.self)
        .image("Square_setting_save")
        .image("Square_setting_disSave", state: .disabled).end()
    private let bgView = c_make_ex(UIView.self)
        .bgColor(.white).end()
    private let contentTextV = c_make_ex(UITextView.self)
        .placeholder("请添加论坛公告").color("#4B4C4D")
        .bgColor(.white).font(15.font_regular).end()
    
    var communityId = ""
    var noticeStr = "" {
        didSet{
            if noticeStr == "未设置" {
                contentTextV.text = ""
            }else{
                let paragraphStyle = NSMutableParagraphStyle()
                paragraphStyle.lineSpacing = 4 // 设置行间距
                let attributedText = NSAttributedString(
                    string: noticeStr,
                    attributes: [
                        .paragraphStyle: paragraphStyle,
                        .font: 15.font_regular!, // 确保设置字体
                        .foregroundColor: "#4B4C4D".toColor // 设置文字颜色（可选）
                    ]
                )
                contentTextV.attributedText = attributedText
            }
            updateSaveButtonState()
        }
    }
    var isHiddenSend = false {
        didSet{
            saveButton.isHidden = isHiddenSend
            contentTextV.isEditable = false
            contentTextV.isSelectable = false
        }
    }
    
    var updateSquareNotice:((_:String)->())?
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            backButton
            titleLabel
            saveButton
            bgView
            contentTextV
        }
        
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        titleLabel.centerHorizontally()
        titleLabel.CenterY == backButton.CenterY
        saveButton.trailing(0).top(.statusHeight).width(77).height(44)
        bgView.leading(0).trailing(0).top(.navi_height).bottom(0)
        contentTextV.leading(16).trailing(16).top(.navi_height).bottom(.safe_bottom)
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        saveButton.addTarget(self, action: #selector(clickSaveButton), for: .touchUpInside)
        
        contentTextV.textContainerInset = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)
        
        NotificationCenter.default.addObserver(self, selector: #selector(textViewDidChange), name: UITextView.textDidChangeNotification, object: contentTextV)
    }
    

    @objc private func clickSaveButton(){
        
        guard let notice = contentTextV.text, notice.count > 0 else {
            "论坛公告不能为空~".toast_error
            return
        }
        Task{
            ToastUtil.show()
            let models = await network.requestSpecial(.editSquareInfo(gid: communityId, field: "notice", value: notice))
            ToastUtil.dismiss()
            if models.isSuccess {
                "修改成功~".toast_success
                self.updateSquareNotice?(notice)
                self.navigationController?.popViewController(animated: true)
            }else{
                models.errorMessage?.toast_error
            }
        }
    }
    
    @objc func textViewDidChange() {
        
        updateSaveButtonState()
    }
    
    private func updateSaveButtonState(){
        if let text = contentTextV.text, text.count > 0 {
            saveButton.isEnabled = true
        }else{
            saveButton.isEnabled = false
        }
    }

}
